package coupon

import (
	"context"
	"gitee.com/enjoy_5/coupons-rpc/internal/server"
	pb "gitee.com/enjoy_5/coupons-rpc/proto/coupon"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

type CouponServer struct {
	pb.UnimplementedCouponsPbServer
}

func (l *CouponServer) CouponListByUserID(ctx context.Context, req *pb.CouponListReq) (*pb.CouponListResp, error) {
	if req.UserId == 0 {
		return nil, status.Error(codes.InvalidArgument, "user id is required")
	}
	CouponShopList := server.TakeCouponShop(int(req.UserId), req.Judge)
	return &pb.CouponListResp{
		CouponList: CouponShopList,
	}, nil
}

func (l *CouponServer) CouponListUserByUserID(ctx context.Context, req *pb.CouponListUserReq) (*pb.CouponListUserResp, error) {
	if req.UserId == 0 {
		return nil, status.Error(codes.InvalidArgument, "user id is required")
	}
	CouponUserList := server.TakeCouponUser(int(req.UserId), int(req.CouponStatus))
	return &pb.CouponListUserResp{
		CouponList: CouponUserList,
	}, nil
}

func (l *CouponServer) CouponAdd(ctx context.Context, req *pb.CouponAddReq) (*pb.CouponAddResp, error) {
	if req.UserId == 0 {
		return nil, status.Error(codes.InvalidArgument, "user id is required")
	}
	if req.PointsId == 0 {
		return nil, status.Error(codes.InvalidArgument, "points id is required")
	}
	_ = server.TakeCouponAdd(int(req.UserId), req.PointsId)
	return &pb.CouponAddResp{}, nil
}

func (l *CouponServer) MemberList(ctx context.Context, req *pb.MemberListReq) (*pb.MemberListResp, error) {
	MemberList := server.TakeList()
	return &pb.MemberListResp{RulesInfo: MemberList}, nil
}

func (l *CouponServer) CouponBatchAdd(ctx context.Context, req *pb.CouponBatchAddReq) (*pb.CouponBatchAddResp, error) {
	if req.Id == 0 {
		return nil, status.Error(codes.InvalidArgument, "user id is required")
	}
	BatchAdd := server.TakeMemberList()
	_ = server.TakeCouponCreate(req.Id, BatchAdd)
	return &pb.CouponBatchAddResp{}, nil
}

func (l *CouponServer) PointsList(ctx context.Context, req *pb.PointsListReq) (*pb.PointsListResp, error) {
	pointsList := server.PointsList()
	return &pb.PointsListResp{PointsList: pointsList}, nil
}

func (l *CouponServer) PointsInfo(ctx context.Context, req *pb.PointsInfoReq) (*pb.PointsInfoResp, error) {
	if req.PointsId == 0 {
		return nil, status.Error(codes.InvalidArgument, "points id is required")
	}
	pointsInfo := server.PointsInfo(int(req.PointsId))
	return &pb.PointsInfoResp{PointsInfoById: pointsInfo}, nil
}

func (l *CouponServer) CouponInfoByID(ctx context.Context, req *pb.CouponInfoByIDReq) (*pb.CouponInfoByIDResp, error) {
	if req.CouponID == 0 {
		return nil, status.Error(codes.InvalidArgument, "coupon id is required")
	}
	infoBYCouponID := server.TakeCouponInfoBYCouponID(req.CouponID)
	return &pb.CouponInfoByIDResp{CouponInfoByID: infoBYCouponID}, nil
}
